<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>供应商管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">供应商名称:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入供应商名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">联系人:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="contacts" placeholder="请输入联系人" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">供应商电话:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phone" placeholder="请输入供应商电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态:</label>
                    <div class="layui-input-inline">
                        <select id="state" name="state">
                            <option value="">可用状态</option>
                            <option value="1">正常</option>
                            <option value="2">停用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="supplier-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!--添加和修改弹出层-->
<div style="display: none;padding: 3px" id="saveOrUpdateDiv">
    <form class="layui-form" id="dataFrm" lay-filter="dataFrm" action="">
        <div class="layui-form-item" style="margin-top: 10px;">
            <div class="layui-inline">
                <label class="layui-form-label">供应商名称:</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id">
                    <input type="text" name="name" lay-verify="required" autocomplete="off"
                           placeholder="请输入供应商名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">联系人:</label>
                <div class="layui-input-inline">
                    <input type="text" name="contacts" lay-verify="required" autocomplete="off"
                           placeholder="请输入联系人" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">手机号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="contactPhone" lay-verify="required|phone" autocomplete="off"
                           placeholder="请输入手机号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">供应商电话:</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" lay-verify="required" autocomplete="off"
                           placeholder="请输入电话"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">银行账号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="account" lay-verify="required|number" autocomplete="off"
                           placeholder="请输入银行账号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">地址:</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" lay-verify="required" autocomplete="off"
                           placeholder="请输入地址"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="state" value="1" title="正常" checked>
                    <input type="radio" name="state" value="2" title="停用">
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="supplier-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<!--数据表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="supplier-table" lay-filter="supplier-table"></table>
    </div>
</div>

<!--表头-->
<script type="text/html" id="supplier-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        批量删除
    </button>
</script>

<!--行工具栏-->
<script type="text/html" id="supplier-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '供应商ID',
                    hide: true,
                    field: 'id',
                    align: 'center',
                },
                {
                    title: '供应商名称',
                    width: 160,
                    field: 'name',
                    align: 'center'
                },
                {
                    title: '联系人',
                    field: 'contacts',
                    align: 'center',
                },
                {
                    title: '联系人手机',
                    field: 'contactPhone',
                    align: 'center',
                },
                {
                    title: '供应商电话',
                    field: 'phone',
                    align: 'center',
                },
                {
                    title: '银行账号',
                    field: 'account',
                    width: 200,
                    align: 'center',
                },
                {
                    title: '地址',
                    field: 'address',
                    align: 'center',
                },
                {
                    title: '状态',
                    field: 'state',
                    align: 'center',
                    templet: function (d) {
                        var strCheck = d.state == "1" ? "checked" : "";
                        return '<input type="checkbox" name="state" lay-skin="switch" lay-text="正常|停用" lay-filter="supplier-enable" ' + strCheck + ' id=' + d.id + '>';
                    }
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                },
                {
                    title: '操作',
                    toolbar: '#supplier-bar',
                    align: 'center'
                }
            ]
        ]

        var tableIns = table.render({
            elem: '#supplier-table',
            url: '/supplier/loadAllSupplierPage',
            page: true,
            cols: cols,
            page: true,
            skin: 'line',
            toolbar: '#supplier-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print'],
            response: {
                statusCode: 200
            },
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            },
        });

        //行监听
        table.on('tool(supplier-table)', function (obj) {
            if (obj.event === 'remove') {
                remove(obj);
            } else if (obj.event === 'edit') {
                edit(obj);
            }
        });

        //表头监听
        table.on('toolbar(supplier-table)', function (obj) {
            if (obj.event === 'add') {
                add();
            } else if (obj.event === 'batchRemove') {
                batchRemove(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //表格监听
        form.on('submit(supplier-query)', function (data) {
            table.reload('supplier-table', {
                where: data.field
            })
            return false;
        });

        //   行 点击监听
        form.on('switch(supplier-enable)', function (obj) {
            var id = $(this).attr('id');
            var state = '';
            obj.elem.checked ? state = '1' : state = '2';
            $.ajax({
                url: "/supplier/updateSupplierState/" + id + "/" + state,
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        });
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })

        });

        //添加
        var mainIndex;
        var url;

        function add() {
            mainIndex = layer.open({
                type: 1,
                title: '添加供应商信息',
                shade: 0.1,
                area: ['650px', '400px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/supplier/addSupplier";
                    $("#dataFrm")[0].reset();
                }
            });
        }


        //修改
        function edit(obj) {
            mainIndex = layer.open({
                type: 1,
                title: '修改供应商信息',
                shade: 0.1,
                area: ['650px', '400px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/supplier/updateSupplier"
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm", obj.data);
                }
            });
        }


        //监听提交按钮
        form.on('submit(supplier-save)', function (data) {
            $.ajax({
                url: url,
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            layer.close(mainIndex);//关闭当前页
                            tableIns.reload();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //删除
        function remove(obj) {
            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/supplier/deleteSupplier/" + obj.data['id'],
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        //批量删除
        function batchRemove(obj) {

            var ids = common.checkField(obj, 'id');

            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/supplier/bathDeleteSupplier/" + ids,
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('supplier-table');
        }

    })
</script>
</body>
</html>
